package com.example.service;


import com.example.entity.Book;
import com.example.entity.Activity;
import com.example.exception.CustomerException;
import com.example.mapper.ActivityMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityService {
    @Autowired
    private BookService bookService;
    @Autowired
    private ActivityMapper activityMapper;
    public List<Activity> selectAll(Activity activity){
        return activityMapper.selectAll(activity);
    }

    public PageInfo<Activity> selectPage(Integer pageNum, Integer pageSize,Activity activity) {
        PageHelper.startPage(pageNum,pageSize);
        List<Activity> list = activityMapper.selectAll(activity);
        return PageInfo.of(list);

    }

    public void add(Activity activity) {
        activity.setNum(0);
        activityMapper.insert(activity);
    }

    public void update(Activity activity) {
        activityMapper.updateById(activity);
    }

    public void deleteById(Integer id) {
//        Book book = bookService.selectByActivityId(id);
//        if (book==null){
//            activityMapper.deleteById(id);
//        }else {
//            throw new CustomerException("该分类下存在书籍，无法删除");
//        }
        activityMapper.deleteById(id);
    }


    @Transactional
    public void deleteBatch(List<Integer> idList) {
        for (Integer id : idList) {
//            activityMapper.deleteById(id);
            this.deleteById(id);
        }
    }

    public Activity selectById(Integer activityId) {
        Activity activity = activityMapper.selectById(activityId);
        return activity;
    }
}
